package nl.rainbowsheep.reisorg.rest.service.controller;

import java.util.ArrayList;
import java.util.List;
import nl.rainbowsheep.reisorg.rest.service.model.Accommodation;
import nl.rainbowsheep.reisorg.rest.service.model.AccommodationHousingBooking;
import nl.rainbowsheep.reisorg.rest.service.service.AccommodationService;
import nl.rainbowsheep.reisorg.rest.service.viewmodel.AccommodationViewmodel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/accommodation")
public class AccommodationController {

    @Autowired
    AccommodationService accommodationService;

    @RequestMapping(value = "/getall", method = RequestMethod.GET)
    @ResponseBody
    public List<AccommodationViewmodel> getAllAccommodationViewmodels() throws Exception {
        List<AccommodationViewmodel> accommodations = accommodationService.getAll();
        return accommodations;
    }

    @RequestMapping(value = "/booking/save", method = RequestMethod.POST)
    @ResponseBody
    public List<AccommodationHousingBooking> saveBookings(@RequestBody List<AccommodationHousingBooking> bookings) {
        List<AccommodationHousingBooking> savedBookings = new ArrayList<AccommodationHousingBooking>();
        for (AccommodationHousingBooking booking : bookings) {
            savedBookings.add(accommodationService.saveBooking(booking));
        }
        return savedBookings;
    }

    @RequestMapping(value = "/find", method = RequestMethod.POST)
    @ResponseBody
    public List<AccommodationViewmodel> findAccommodation(@RequestBody AccommodationViewmodel accommodationViewmodel) {
        List<AccommodationViewmodel> accommodations = accommodationService.findAccommodations(accommodationViewmodel);
        return accommodations;
    }

    /**
     * Required for testing purposes.
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getaccommodation", method = RequestMethod.GET)
    public AccommodationViewmodel getTrip(@RequestParam(value = "id", required = true) long id) throws Exception {
        AccommodationViewmodel acc = null;
        try {
            acc = accommodationService.findOne(id);
        } catch (Exception e) {
            throw new Exception(e.toString());
        }
        return acc;
    }

    /**
     * Method accepts an AccommodationViewmodel object to save in database.
     * @param locale
     * @param accommodationViewmodel
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Accommodation saveAccommodationViewmodel(@RequestParam(value = "locale", required = true) String locale, @RequestBody AccommodationViewmodel accommodationViewmodel) throws Exception {
        try {
            return accommodationService.save(accommodationViewmodel);
        } catch (Exception ex) {
            throw new Exception(ex.toString());
        }
    }

}
